﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVCFoto.IDAL;
using MVCFoto.Model;
using System.Web.Security;
using System.Web;

namespace MVCFoto.BLL
{
    public class User
    {//TODO:完善缓存的更新
        private static readonly IUser dal = MVCFoto.DALFactory.Factory.Create<IUser>("User");

        public UserInfo GetUser(int id)
        {
            return dal.GetUser(id);
        }
        private static object userInfoLock = new object();
        public UserInfo GetUser(string username)
        {
            return CacheHelper.GetCacheItem<UserInfo>(String.Format("MF_Cache_UserInfo_{0}", username),
                userInfoLock,
                TimeSpan.FromMinutes(10),
                delegate
                {
                    return dal.GetUser(username);
                });
        }

        public UserInfo GetUser(string username, string password)
        {
            return dal.GetUser(username,password);
        }
        public int AddUser(UserInfo ui,out CreateUserStatus status)
        {
            status = CreateUserStatus.Unknow;
            if (GetUser(ui.UserName) != null)
                status = CreateUserStatus.DuplicateUserName;
            if (MVCFotoSettings.RequiresUniqueEmail && GetUserByEmail(ui.Email) != null)
                status = CreateUserStatus.DuplicateEmail;
            
            int userId = dal.AddUser(ui);
            if (userId > 0)
                status = CreateUserStatus.Success;
            //else
            //    status = CreateUserStatus.Unknow;
            return userId;
        }
        public bool UpdateUser(UserInfo ui)
        {
            return dal.UpdateUser(ui);
        }
        public bool ChangePassword(int userId, string password)
        {
            return dal.ChangePassword(userId,password);
        }
        public UserInfo GetUserByEmail(string email)
        {
            return dal.GetUserByEmail(email);
        }

        /// <summary>
        /// 当前登录的用户ID
        /// </summary>
        public static int MyID
        {
            get {
                if (!HttpContext.Current.User.Identity.IsAuthenticated)
                    return -1;
               
                User u = new User();
                UserInfo ui = u.GetUser(HttpContext.Current.User.Identity.Name);
                return ui.UserID;
            }
        }
        /// <summary>
        /// 我的信息
        /// </summary>
        public static UserInfo MyInfo
        {
            get {
                if (!HttpContext.Current.User.Identity.IsAuthenticated)
                    return null;
                User u = new User();
                return  u.GetUser(HttpContext.Current.User.Identity.Name);                
            }
        }

        //登录
        public static bool Login(string username, string password,bool remindme)
        {
            password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
            User u = new User();
            UserInfo user =  u.GetUser(username, password);
            if (user == null)
                return false;
            else
            {
                //更新用户最后登录信息
                user.LastIP = HttpContext.Current.Request.UserHostAddress;
                user.LastLogin = DateTime.Now;
                user.Logins = 1;
                u.UpdateUser(user);
                FormsAuthentication.SetAuthCookie(user.UserName, remindme);
                //HttpContext.Current.Response.Cookies.Add(new HttpCookie());
                return true;
            }
        }
        public IList<UserInfo> GetUserFriends(int userId)
        {
            return dal.GetUserFriends(userId);
        }
        public enum CreateUserStatus
        {
             Unknow,
             DuplicateUserName,
             DuplicateEmail,
             InvalidPassword,
             InvalidEmail,
             InvalidUserName,
             UserRejected,
            Success
                   
        }
        /// <summary>
        /// 个人隐私设定
        /// </summary>
        public enum Privacy
        {
            Public = 0,
            Friend = 1,
            Private = 2
        }
       
    }
}
